/*   
 * Copyright (c) 2011, TUgether, TU Braunschweig
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
 /**
 * @author Mohamed Karray <mohamed.karray@gmx.de>
 *
 */
 

package de.tu_bs.tugether.liferayunit.liferayElements.portlets;

import java.io.IOException;

import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.javascript.host.Event;

import de.tu_bs.tugether.liferayunit.exceptions.LiferayElementNotFoundException;
import de.tu_bs.tugether.liferayunit.exceptions.LiferayUnitException;
import de.tu_bs.tugether.liferayunit.liferayPages.LiferayPage;
import de.tu_bs.tugether.liferayunit.liferayPages.MaximizedPortletPage;

public class LoginPortlet extends Portlet {
	
	protected static final String XPATH_KEY_USERNAME_FIELD = "usernameField";	
	protected static final String XPATH_KEY_PASSWORD_FIELD = "passwordField";	
	protected static final String XPATH_KEY_SUBMITT_BUTTON = "submitButton";	
	protected static final String XPATH_KEY_ERROR_DIV = "errorDiv";		

	HtmlTextInput usernameField ;
	HtmlPasswordInput passwordField ;
	HtmlSubmitInput  submitButton ;
	HtmlDivision errorDiv ;
	
	public LoginPortlet(HtmlElement htmlElement, LiferayPage liferayPage) throws IOException, LiferayElementNotFoundException {
		super(htmlElement, liferayPage);
		
	}
	
	protected void initialize() throws LiferayElementNotFoundException  {
		super.initialize();
		try{
			usernameField = super.<HtmlTextInput>getFirstByXPathKey(XPATH_KEY_USERNAME_FIELD);
		}catch(LiferayUnitException e){
			
		}
		try{
			passwordField = super.<HtmlPasswordInput>getFirstByXPathKey(XPATH_KEY_PASSWORD_FIELD);
		}catch(LiferayUnitException e){
		}
		try{
			submitButton = super.<HtmlSubmitInput>getFirstByXPathKey(XPATH_KEY_SUBMITT_BUTTON);
		}catch(LiferayUnitException e){
		}
		try{
			errorDiv = super.<HtmlDivision>getFirstByXPathKey(XPATH_KEY_ERROR_DIV);
		}catch(LiferayUnitException e){
			errorDiv = null;
		}
	}
	
	public HtmlPage login(String username,String password) throws Exception{
		this.usernameField.setValueAttribute(username);
		this.passwordField.setValueAttribute(password);
		final Event event = new Event(this.submitButton, Event.TYPE_SUBMIT);
		HtmlPage page2 = this.submitButton.click(event);
		//HtmlPage page2 = this.submitButton.click();
		//System.out.println("\n"+LiferayUtil.isSignedIn(page2)+page2.asText());
		
		//System.out.println("hier wird eingelogt - die weitergeleitete Seite ist : "+page2.asText());
	
		return page2;
		
	}
	
	@Override
	public MaximizedPortletPage<LoginPortlet> maximize() throws Exception{
		return super.<LoginPortlet>maximize();
	}
		
	
/*	
	public MainPage login(String username,String password){
		LiferayElement form = this.getLiferayElements(ELEMENT_ID_LOGIN_FORM).get(0);
		LiferayElement username_input = this.getLiferayElements(ELEMENT_ID_USERNAME_INPUT).get(0);
		LiferayElement password_input = this.getLiferayElements(ELEMENT_ID_PASSWORD_INPUT).get(0);
		username_input.<HtmlTextInput>getHtmlElement().setValueAttribute(username);
		password_input.<HtmlPasswordInput>getHtmlElement().setValueAttribute(password);
		
		HtmlSubmitInput button = form.<HtmlForm>getHtmlElement().getInputByName("submit");
		
		//final HtmlPage page = submit.click();
		
		//TODO Login f?r Liferay ist ein Portlet miximiert auf eine Seite: also von Typ Portlet mit
		// warning Message und footer Message
		
		HtmlPage page = button.<HtmlPage>click();
		MainPage mp = new MainPage( page,null);
		try{
			mp.getLiferayElements(ELEMENT_ID_LOGIN_FORM).get(0);
			return mp;
		}catch(LiferayElementNotFoundException e){
			
			return null;
		}
	}
	*/

	public String getErrorMessage() {
		if (this.errorDiv != null){
			return errorDiv.asText();
		}
		return "No Error";
	}
}
